package cn.jcet.module.sys.controller;


import cn.jcet.common.utils.DataGridView;
import cn.jcet.common.utils.ResultObj;
import cn.jcet.module.sys.domain.LogInfo;
import cn.jcet.module.sys.service.ILogInfoService;
import cn.jcet.module.sys.vo.LogInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 鲜磊
 * @since 2019-12-16
 */
@RestController
@RequestMapping("/logInfo")
public class LogInfoController {

    @Autowired
    private ILogInfoService ILogInfoService;


    @RequestMapping("loadAllLogInfo")
    public DataGridView loadAllLogInfo(LogInfoVo logInfoVo){
        //分页
        IPage<LogInfo> page = new Page<>(logInfoVo.getPage(),logInfoVo.getLimit());
        //查询全部
        QueryWrapper<LogInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(logInfoVo.getLoginname()),"loginname",logInfoVo.getLoginname());
        queryWrapper.like(StringUtils.isNotBlank(logInfoVo.getLoginip()),"loginip",logInfoVo.getLoginip());
        queryWrapper.ge(logInfoVo.getStartTime() != null,"logintime",logInfoVo.getStartTime());
        queryWrapper.le(logInfoVo.getEndTime()!=null,"logintime",logInfoVo.getEndTime());
        //排序
        queryWrapper.orderByDesc("logintime");
        this.ILogInfoService.page(page,queryWrapper);
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    @RequestMapping("deleteLogInfo")
    public ResultObj deleteLogInfo(Integer id){
        try {
            this.ILogInfoService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    @RequestMapping("batchDeleteLogInfo")
    public ResultObj batchDeleteLogInfo(LogInfoVo LogInfoVo){

        try {
            Collection<Serializable> idList = new ArrayList<>();
            for (Integer id: LogInfoVo.getIds()){
                idList.add(id);
            }
            this.ILogInfoService.removeByIds(idList);
            return  ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }
}
